<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item label="参数名称">
            <el-input v-model="name" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="permissions.sdAdmin_param_get" icon="el-icon-search" type="primary"
                      @click="getList">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true">
        <el-form-item>
          <el-button v-if="permissions.sdAdmin_param_get" icon="el-icon-plus" type="primary"
                     @click="addOrUpdateHandle(false)">
            添加
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        border
        v-loading="loading"
        :data="paramList"
        row-key="id"
        lazy
        current-row-key="name"
        :load="treeLoad"
        :key="Math.random()"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column type="index" label="编号" width="60"></el-table-column>
        <!-- <el-table-column prop="id" label="序号" :show-overflow-tooltip="true" width="100"></el-table-column> -->
        <el-table-column prop="name" label="参数名称" :show-overflow-tooltip="true" width="200"></el-table-column>
        <el-table-column prop="value" label="参数值" :show-overflow-tooltip="true" width="180"></el-table-column>
        <el-table-column prop="sequence" label="排序" width="60"></el-table-column>
        <el-table-column prop="status" label="状态" width="80" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status === 'ZT10001'">显示</el-tag>
            <el-tag type="warning" v-if="scope.row.status === 'ZT10002'">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false,scope.row.id)"
              v-if="permissions.sdAdmin_param_get">添加
            </el-button>
            <el-button type="text"
                       icon="el-icon-edit"
                       @click="addOrUpdateHandle(true,scope.row.id)"
                       v-if="permissions.sdAdmin_param_edit">修改
            </el-button>
            <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-if="permissions.sdAdmin_param_del">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></table-form>
    </div>
  </basic-container>
</template>

<script>
  import {fetchList, fetchParamTree, getObj, addObj, putObj, delObj} from '@/api/sdAdmin/param'
  import TableForm from './param-form'
  import {mapGetters} from 'vuex'

  export default {
    name: "sdAdmin-param",
    components: {TableForm},
    data() {
      return {
        addOrUpdateVisible: false,
        // 遮罩层
        loading: true,
        // 菜单表格树数据
        paramList: [],
        // 菜单树选项
        paramOptions: [],
        name: null,
        maps: new Map()
      };
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters(['permissions']),
    },
    methods: {
      addOrUpdateHandle(isEdit, id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(isEdit, id)
        })
      },
      getList() {
        this.loading = true;
        fetchParamTree(true,-1,this.name).then(response => {
          this.paramList = response.data.data
          this.loading = false;
        });
      },
      handleDelete(row) {
        this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delObj(row.id);
        }).then(() => {
          this.getList();
          const {parentId} = row  //取出当前删除行的pid
          const { tree, treeNode, resolve } = this.maps.get(parentId) //根据pid取出对应的节点数据
          // this.$set(this.$refs.table.store.states.lazyTreeNodeMap, parentId, []); //将对应节点下的数据清空，从而实现数据的重新加载
          this.treeLoad( tree, treeNode, resolve )
          this.$message.success('删除成功')
        })
      },
      treeLoad(tree, treeNode, resolve) {
        const pid = tree.id
        this.maps.set(pid,{ tree, treeNode, resolve })  //将当前选中节点数据存储到maps中
        fetchParamTree(true,tree.id,this.name).then(response => {
          resolve(response.data.data)
        })
      }
    }
  };
</script>
